﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MouseCalib
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try {
                serialPort1.Open();
            }
            catch (Exception ex)
            {
                showException(ex);
            }
        }

        private void showException(Exception ex)
        {
            showInfo($"Error:{ex.Message}");
        }

        const int showLines = 45;
        private void showInfo(string exmsg)
        {
            textBox1.AppendText($"{exmsg}\r\n");
            if (textBox1.Lines.Length > showLines) {
                textBox1.Lines = textBox1.Lines.Skip(textBox1.Lines.Length - showLines).Take(showLines).ToArray();
            }
        }

        const byte Zero = 80;
        int lastX=-1;
        int lastY=-1;
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.chkMouse.Checked)
            {
                //if (e.X > lastX) sendstr("mr");
                //if (e.X < lastX) sendstr("ml");
                //if (e.Y > lastY) sendstr("md");
                //if (e.Y < lastY) sendstr("mu");
                if (lastX == -1 || lastY == -1)
                    sendMove(0, 0);
                else
                    sendMove(e.X - lastX,e.Y - lastY);


                lastX = e.X;
                lastY = e.Y;
            }
        }

        private int limitValue(int deltaX)
        {
            if (deltaX > 45) deltaX = 45;
            if (deltaX < -45) deltaX = -45;
            return deltaX;
        }

        private void sendByteArr(byte[] s)
        {

            if (serialPort1.IsOpen)
            {
                this.serialPort1.Write(s,0,s.Length);
            }
            else
            {
                showInfo("串口没开");
            }
        }

        private void sendbyte(byte b)
        {
            byte[] buffer = { b, 3 };
            sendByteArr(buffer);
        }



        private void Form1_Click(object sender, EventArgs e)
        {
            sendbyte((byte)'1');        //左键
            //sendbyte((byte)'2');        //右键
            //sendbyte((byte)'3');        //中间键
        }


        int ic = 0;
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string str = serialPort1.ReadExisting();//字符串方式读
            textBox1.Invoke(new Action(()=> textBox1.AppendText($"COM : {str}")));

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            showInfo($"Keycode:{e.KeyCode}, KeyValue:{e.KeyValue}");
            //sendbyte((byte)e.KeyValue);
            if (e.KeyCode == Keys.A)
            {
                sendLeftMouseDown();
            }
            else if (e.KeyCode == Keys.B)
            {
                sendLeftMouseUp();
            }
            else if (e.KeyCode == Keys.C)
            {
                sendClick();
            }
            else if (e.KeyCode == Keys.D)
            {
                sendDblClick();
            }
            else if (e.KeyCode == Keys.Right)
            {
                sendForward();
            }
            else if (e.KeyCode == Keys.Left)
            {
                sendBackward();
            }
            else if (e.KeyCode == Keys.Home)
            {
                sendMouseCenter();
            }
        }

        private void sendForward()
        {
            sendLeftMouseDown();
            sendMove(20, 0);
            sendMove(20, 0);
            sendMove(20, 0);
            sendLeftMouseUp();
            sendMouseCenter();
        }

        private void sendMouseCenter()
        {
            //恢复到 0,0
            for (int i = 0; i < 10; i++)
            { 
                sendMove(-30, -30);
                Thread.Sleep(10);
            }
            for (int i = 0; i < 10; i++)
            {
                sendMove(20, 10);
                Thread.Sleep(10);
            }
                
        }

        private void sendBackward()
        {
            sendLeftMouseDown();
            sendMove(-20, 0);
            sendMove(-20, 0);
            sendMove(-20, 0);
            sendLeftMouseUp();
            sendMouseCenter();
        }

        private void sendMove(int dx,int dy)
        {
            byte[] s = { (byte)'m', Zero, Zero, 3 };
            
            int deltaX = limitValue(dx);
            int deltaY = limitValue(dy);

            s[1] = (byte)(Zero + deltaX);
            s[2] = (byte)(Zero + deltaY);
            
            sendByteArr(s);
        }




        private void sendClick()
        {
            sendLeftMouseDown();
            sendLeftMouseUp();
        }
        private void sendDblClick()
        {
            sendLeftMouseDown();
            Thread.Sleep(20);
            sendLeftMouseUp();
            Thread.Sleep(50);
            sendLeftMouseDown();
            Thread.Sleep(20);
            sendLeftMouseUp();
        }

        private void sendLeftMouseDown()
        {
            sendByteArr(new byte[] { (byte)'p', (byte)'1' ,(byte)3 });
        }
        private void sendLeftMouseUp()
        {
            sendByteArr(new byte[] { (byte)'r', (byte)'1', (byte)3 });
        }
        private void sendRightMouseDown()
        {
            sendByteArr(new byte[] { (byte)'p', (byte)'2', (byte)3 });
        }
        private void sendRightMouseUp()
        {
            sendByteArr(new byte[] { (byte)'r', (byte)'2', (byte)3 });
        }


        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            showInfo("--MouseDown--");
            if (e.Button == MouseButtons.Left)
                sendLeftMouseDown();
            else if (e.Button == MouseButtons.Right)
                sendRightMouseDown();
            else
                sendByteArr(new byte[] { (byte)'p', (byte)'3',(byte)3 });
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            showInfo("--MouseUp--");
            if (e.Button == MouseButtons.Left)
                sendLeftMouseUp();
            else if (e.Button == MouseButtons.Right)
                sendRightMouseUp();
            else
                sendByteArr(new byte[] { (byte)'r', (byte)'3', (byte)3 });
        }
    }
}
